#include "chooselevescene.h"
#include <QMenuBar>
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include <qlabel.h>
#include <QSound>

ChooseLeveScene::ChooseLeveScene(QWidget *parent) : QMainWindow(parent)
{
    // 配置选择关卡场景
    this->setFixedSize(320, 588);

    // 设置图标
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    // 设置标题
    this->setWindowTitle("选择关卡场景");

    // 创建菜单栏
    QMenuBar *bar = menuBar();
    setMenuBar(bar);

    // 创建开始菜单
    QMenu *startMenu = bar->addMenu("开始");

    // 创建退出
    QAction *quitAction = startMenu->addAction("退出");

    // 点击退出
    connect(quitAction, &QAction::triggered, [=](){
        this->close();
    });

    QSound *chooseSound = new QSound(":/res/TapButtonSound.wav", this);
    QSound *backSound = new QSound(":/res/BackButtonSound.wav", this);
    // 返回按钮
    MyPushButton * backBtn = new MyPushButton(":/res/BackButton.png", ":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(), this->height()-backBtn->height());

    connect(backBtn, &QPushButton::clicked, [=](){
        // qDebug()<<"返回";
        backSound->play();
        QTimer::singleShot(300, this, [=](){
            emit this->chooseSceneBack();
        });
    });


    for(int i=0;i<20;++i) {
        MyPushButton * menuBtn = new MyPushButton(":/res/LevelIcon.png");
        //menuBtn->setText(QString::number(i+1));
        menuBtn->setParent(this);

        menuBtn->move(25+i%4*70, 130+i/4*70);

        // 关卡点击事件
        connect(menuBtn, &QPushButton::clicked, [=](){
            chooseSound->play();
            QString str = QString("您选择的是第%1关").arg(i+1);
            qDebug()<<str;

            this->hide();
            // 进入游戏场景
            play = new PlayScene(i);
            play->setGeometry(this->geometry());
            play->show();

            connect(play, &PlayScene::chooseSceneBack, [=](){
                this->setGeometry(play->geometry());
                delete play;
                play=NULL;
                this->show();
            });
        });

        QLabel *lbl = new QLabel;
        lbl->setParent(this);
        lbl->setFixedSize(menuBtn->width(), menuBtn->height());
        lbl->setText(QString::number(i+1));
        lbl->move(25+i%4*70, 130+i/4*70);

        // 设置label 文字对齐
        lbl->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        //lbl->setMouseTracking()
        lbl->setAttribute(Qt::WA_TransparentForMouseEvents);
    }
}


void ChooseLeveScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,width(), height(),pix);

    // 背景图标
    pix.load(":/res/Title.png");
    painter.drawPixmap((width()-pix.width())/2,30,pix.width(), pix.height(), pix);
}

